<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\Chat\Controller\ChatBaseController;
use App\Chat\Service\TalkService;
use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Dto\TalkRecordExcelDto;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller('ch/talk/record'),ApiAuth,VerifySignature]
class TalkRecordController extends ChatBaseController
{

    public array $noNeedLogin = ['export'];
    public array $noNeedVerifySign = ['export'];

    #[Inject]
    protected TalkService $service;

    #[PostMapping('list')]
    public function list()
    {
        return $this->success($this->service->list($this->request->all()));
    }

    #[PostMapping('details')]
    public function details(): \Psr\Http\Message\ResponseInterface
    {
        $params = $this->request->inputs(['talk_type','robot_id', 'receiver_id', 'record_id','msg_type','keyword']);

        $this->validate($params, [
            'talk_type'   => 'required|in:1,2',
            'robot_id'    => 'required',
            'receiver_id' => 'required|integer|min:1',
        ]);

        $msgType = !empty($params['msg_type']) ?  json_decode($params['msg_type'],true) : [];
        $keyword = $params['keyword'] ?? '';

        $limit  = 30;
        $result = $this->service->getChatRecords(
            intval($params['robot_id']),
            $params['receiver_id'],
            intval($params['talk_type']),
            intval($params['record_id']),
            $limit,
            $msgType,
            $keyword
        );

        return $this->success([
            'rows'      => $result,
            'record_id' => $result ? array_shift($result)['id'] : 0,
            'limit'     => $limit
        ]);
    }

    #[GetMapping('export')]
    public function export()
    {
        return $this->service->export($this->request->all(),TalkRecordExcelDto::class);
    }
}